using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.TaskManage.Dto;
using ZR.Model.TaskManage;
using ZR.Repository;
using ZR.Service.TaskManage.ITaskManageService;

namespace ZR.Service.TaskManage
{
    /// <summary>
    /// 拣选任务Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ITaskPickService), ServiceLifetime = LifeTime.Transient)]
    public class TaskPickService : BaseService<TaskPick>, ITaskPickService
    {
        /// <summary>
        /// 查询拣选任务列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<TaskPickDto> GetPageList(TaskPickQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<TaskPick, TaskPickDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="PickId"></param>
        /// <returns></returns>
        public TaskPick GetInfo(string PickId)
        {
            var response = Queryable()
                .Where(x => x.PickId == PickId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加拣选任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public TaskPick AddTaskPick(TaskPick model)
        {
            //生成拣选任务id
            string maxPickId = Queryable().Max(it => it.PickId);
            if (string.IsNullOrEmpty(maxPickId))
            {
                model.PickId = DateTime.Now.ToString("yyyyMMdd");
                model.PickId += "-";
                model.PickId += "0001";
            }
            else
            {
                string time = DateTime.Now.ToString("yyyyMMdd");
                if (maxPickId.StartsWith(time))
                {
                    int num = maxPickId.Split('-')[1].TrimStart('0').ParseToInt();
                    num++;
                    model.PickId = time + "-" + num.ToString().PadLeft(4, '0');
                }
                else
                {
                    model.PickId = DateTime.Now.ToString("yyyyMMdd");
                    model.PickId += "-";
                    model.PickId += "0001";
                }
            }


            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改拣选任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateTaskPick(TaskPick model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<TaskPick> QueryExp(TaskPickQueryDto parm)
        {
            var predicate = Expressionable.Create<TaskPick>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PickId), it => it.PickId == parm.PickId);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PickState), it => it.PickState == parm.PickState);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PlanCode), it => it.PlanCode == parm.PlanCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PickCode), it => it.PickCode == parm.PickCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PickSystype), it => it.PickSystype == parm.PickSystype);
            return predicate;
        }
    }
}